package com.gzy.xuexiziliao.controller;


import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Admin;
import com.gzy.xuexiziliao.service.AdMinService;

import com.gzy.xuexiziliao.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
/**
 * 管理员登录
 */
@RestController
@Slf4j
public class AdminLoginController {
    @Autowired
    private AdMinService adminService;

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        log.info("管理员登录：{}", admin);
        Admin a = adminService.login(admin);
        //获取jwt令牌
        if (a != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", a.getId());
            claims.put("name", a.getName());
            claims.put("username", a.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }

}
